﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Arcanea.NetworkEngine.Abstract.Engines;
using Arcanea.NetworkEngine.Concrete;
using Arcanea.Mediator.FullClasses;

namespace Arcanea.MMORPGServer
{
    public class ThreadItem
    {
        private object objKey = new object();

        private Thread th = null;
        private NetInformationContainer infos = null;
        private bool continu = true;

        TCPClientOfServerNetEngine _neClient;

        private NetInformationContainer _Infos;
        public NetInformationContainer Infos
        {
            get { return _Infos; }
            set { _Infos = value; _neClient = new TCPClientOfServerNetEngine(infos); }
        }


        public ThreadItem(NetInformationContainer infos)
        {
            this.infos = infos;
        }

        public void Start()
        {
            if (th == null)
            {
                th = new Thread(new ThreadStart(ThreadBody));
                th.Start();
            }
        }

        public void Stop()
        {
            lock (objKey)
            {
                continu = false;
            }
        }

        public bool IsActive()
        {
            bool continuObject = true;
            lock (objKey)
            {
                continuObject = continu;
            }
            return continuObject;
        }

        private void ThreadBody()
        {
            FullPacket fp = null;
            bool continuObject = true;
            while (continuObject)
            {
                //Corps du Thread.
                fp = (FullPacket)_neClient.Receive();


                lock (objKey)
                {
                    continuObject = continu;
                }
            }
        }
    }
}
